<?php

class Reservations_View_List {

    private $resource_type_id;

    function __construct($resource_type_id) {
        $this->resource_type_id = $resource_type_id;
    }

    public function toString() {
        $view_wrapper = new Div_Container('id', 'reservations-wrapper');
        $heading = new Div_Container('id', 'View_Heading');
        $heading->addContent('<h1>Reservations Page</h1></br></br>');
        $body = new Div_Container('id', 'reservations-body');

        $attrs = array('width' => '680', 'class' => 'gridtable');
        $table = new HTML_Table($attrs);
        $table->setAutoGrow(true);
        $table->setAutoFill('n/a');
        $table->setHeaderContents(0, 0, ' ');
        $table->setHeaderContents(0, 1, ' ');
        $table->setHeaderContents(0, 2, 'Resource Name');
        $table->setHeaderContents(0, 3, 'User Name');
       
        $table->setHeaderContents(0, 4, 'Time');
        $table->setHeaderContents(0, 5, 'Date');
        $table->setHeaderContents(0,6, ' ');

        /* $edit_btn = '<button onclick=window.location.href=' . '\'index.php?action=reservation_edit&id=11872394\'' .
          ' type="button" name="edit" id="moreReservations">edit</button>'; */
        $delete_btn = '<input class="delete_btn" type="submit" value="Delete Selected" name="" />';

        $reservation_inventory = ReservationDB::getReservationsByResourceTypeID($this->resource_type_id);
        $data = array();
        $current_item = new Reservation();
        if (!is_null($reservation_inventory)) {
            $reservation_inventory->rewind();
            while ($reservation_inventory->valid()) {
                $current_item = $reservation_inventory->current();
                $reservation_id = $current_item->getReservation_id();
                $note = $current_item->getReservation_note();
                $timestamp = $current_item->getReservation_timestamp();
                $resource_id = $current_item->getResource_id();
                $resource_type_id = $current_item->getResource_type_id();
                $slot_date = $current_item->getSlot_date();
                $slot_id = $current_item->getSlot_id();
                $slots_group_id = $current_item->getSlots_group_id();
                $user_id = $current_item->getUser_id();

                $slot = SlotDB::getSlotByID($slot_id);
                $resource = ResourceDB::getResourceByID($resource_id);
                $user = UserDB::getUserByID($user_id);

                $checkbox = '<input type="checkbox" name="reservation_grp_id[]" value="' . $reservation_id . '" />';
                $more_btn = '<button onclick=window.location.href=' . '\'index.php?action=reservation_detail&reservation_id=' . $reservation_id .'\'' .
                        ' type="button" name="more" id="moreReservations">more</button>';

                $table_row_data = array($checkbox, $resource->getResource_name(),
                    $user->getName(), 
                    $slot->getSlot_time_from() . ' - ' . $slot->getSlot_time_to(),
                    $slot_date, $more_btn);
                $data[] = $table_row_data;
                $reservation_inventory->next();
            }
            for ($nr = 0; $nr < count($data); $nr++) {
                $table->setHeaderContents($nr + 1, 0, (string) $nr + 1);
                for ($i = 0; $i < 6; $i++) {
                    if ('' != $data[$nr][$i]) {
                        $table->setCellContents($nr + 1, $i + 1, $data[$nr][$i]);
                    }
                }
            }
        } else {
            //no product types
        }

        $form_head = '<form method="post" id="reservations_list_form" action="' . $_SERVER['PHP_SELF'] . '">' .
                '<input type="hidden" name="action" id="action-0" value="reservations_list" />'.
                 '<input type="hidden" name="resource_type_id" id="action-0" value="' . $this->resource_type_id .'" />';
        $body->addContent($form_head);
        $body->addContent($table->toHtml());
        $body->addContent($delete_btn);
        $body->addContent("</form>");


        

        $view_wrapper->addContent($heading->toString());
        $view_wrapper->addContent($body->toString());

        return $view_wrapper->toString();
    }

}

?>
